import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../util/token'
import { Toast } from 'vant'
import store from '../store/index'
import { getProfile } from '../api/user'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../views/login')
  },
  {
    path: '/home',
    component: () => import('../views/home/index.vue'),
    redirect: '/index',
    children: [
      {
        // 主页
        path: '/detail/:id',
        name: 'detail',
        component: () => import('../views/home/detail/index.vue')
      },
      {
        // 主页
        path: '/index',
        name: 'index',
        component: () => import('../views/home/index/index.vue'),
        meta: {
          needBar: true,
          needLogin: true
        }
      },
      {
        // 视频
        path: '/video',
        name: 'video',
        component: () => import('../views/home/video'),
        meta: {
          needBar: true,
          needLogin: true
        }
      },
      {
        // 个人
        path: '/my',
        name: 'my',
        component: () => import('../views/home/my/index.vue'),
        meta: {
          needBar: true,
          needLogin: true
        }
      },
      {
        // 搜索
        path: '/search',
        name: 'search',
        component: () => import('../views/home/search/index.vue'),
        meta: {
          needBar: true,
          needLogin: true
        }
      },
      {
        // 搜索结果页面
        path: '/searchResult/:q',
        name: 'searchResult',
        component: () => import('../views/home/search/searchResult.vue'),
        meta: {
          needBar: true,
          needLogin: true
        }
      },
      {
        // 修改个人信息页面
        path: '/info',
        component: () => import('../views/home/my/info.vue'),
        meta: { needLogin: true }
      },
      {
        // 收藏与历史页面
        path: '/collection/:active',
        component: () => import('../views/home/my/collection.vue'),
        meta: { needLogin: true }
      },
      {
        //  作品页面
        path: '/works',
        component: () => import('../views/home/my/works.vue'),
        meta: { needLogin: true }
      },
      {
        path: '/chat',
        name: 'chat',
        component: () => import('../views/home/my/chat.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach(async (to, from, next) => {
  var needLogin = to.meta.needLogin
  var token = getToken()
  // 判断 needLogin
  if (needLogin) {
    // 需要登录：判断 token
    if (token) {
      // token 存在：判断 user 中是否存在数据
      if (store.state.isLogin) {
        // 向下执行
        next()
      } else {
        // 根据 token 重新得到用户信息保存到 vuex 中
        const res = await getProfile(token)
        // 重新将用户信息存入到 vuex 中
        store.commit('setUser', res.data.data)
        store.commit('setLogin', true)
        // 向下执行
        next()
      }
    } else {
      // token 不存在
      // 提示未登录，跳转到登录页面
      Toast.fail('对不起，您还未登录')
      next('/login?_redirect=' + to.fullPath)
    }
  } else {
    // 不需要登录：向下执行
    next()
  }
})
export default router
